package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.DwaQuestionRecord;
import cn.com.connext.scrm.service.DwaQuestionRecordService;
import cn.com.connext.scrm.service.DwaQuestionnaireService;
import cn.com.connext.scrm.service.PrizeGoodService;
import cn.com.connext.scrm.service.WxAPIService;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.ProductService;
import cn.com.connext.scrm.service.mapper.TmpMsgRecordService;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.req.*;
import cn.com.connext.scrm.web.rest.resp.ProductResp;
import cn.com.connext.scrm.web.rest.vm.DwaQuestionRecordVM;
import cn.com.connext.scrm.web.rest.vm.DwaQuestionnaireVM;
import cn.com.connext.scrm.web.rest.vm.TempSubScribeVm;
import com.alibaba.fastjson.JSON;
import com.connext.common.exception.ServiceException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

;import java.util.List;

/**
 * 问卷调查
 */
@RestController
@RequestMapping("/api/shopping/dwaquestion")
public class DwaQuestionRecordResource {

    private final Logger log = LoggerFactory.getLogger(DwaQuestionRecordResource.class);

    @Autowired
    private DwaQuestionRecordService dwaQuestionRecordService;
    @Autowired
    private DwaQuestionnaireService dwaQuestionnaireService;

    /**
     * 查询当前用户回答到哪一道题
     */
    @PostMapping("/findQuestionByUnionId")
    public JsonResult findQuestionByUnionId(@RequestBody DwaQuestionRecordVM vm) {
        JsonResult result =new JsonResult();
        try {
            DwaQuestionRecord dwaQuestionRecord = dwaQuestionRecordService.findQuestionByUnionId(vm.getUnionId());
            if (dwaQuestionRecord != null) {
                result.setData(dwaQuestionRecord.getQuestionId());
            } else {
                result.setData(0);
            }
        }catch(Exception e){
            log.error("e:{}",e.getMessage());
        }
        result.setState(0);
        result.setMessage("数据如上");
        return result;
    }


    /**
     * 添加用户回答问卷信息
     */
    @PostMapping("/saveDwaQuestionRecord")
    public JsonResult saveDwaQuestionRecord(@RequestBody DwaQuestionRecordVM vm) {
        JsonResult result =new JsonResult();
        try{
            result = dwaQuestionRecordService.save(vm);
        }catch (Exception e){
            log.error("e:{}",e.getMessage());
        }
        result.setState(0);
        result.setMessage("添加成功");
        return result;
    }

    /**
     * 添加用户点击问卷的次数
     */
    @PostMapping("/saveDwaQuestionnaire")
    public JsonResult saveDwaQuestionnaire(@RequestBody DwaQuestionnaireVM vm) {
        JsonResult result =new JsonResult();
        try{
            result = dwaQuestionnaireService.save(vm);
        }catch (Exception e){
            log.error("e:{}",e.getMessage());
        }
        result.setState(0);
        result.setMessage("添加成功");
        return result;
    }

    /**
     * 查询用户点击问卷次数,type =1 表示用户不去重
     */
    @PostMapping("/findUserClickQuestionNaire")
    public JsonResult findUserClickQuestionNaire(@RequestBody DwaQuestionRecordVM vm) {
        JsonResult result =new JsonResult();
        Integer count  = 0;
        try{
            if(vm.getType() == 1){
                 count  = dwaQuestionnaireService.findUserClickQuestionNaire();
            }else{
                List<String> list =  dwaQuestionnaireService.findNoRepeatUserClickQuestionNaire();
                if(list !=null){
                    count =list.size();
                }
            }
        }catch (Exception e){
            log.error("e:{}",e.getMessage());
        }
        result.setData(count);
        result.setState(0);
        result.setMessage("数据如上");
        return result;
    }

}
